package com.cma.cmabackend.service;

import com.cma.cmabackend.dao.LabEmpDao;
import com.cma.cmabackend.dao.UserDao;
import com.cma.cmabackend.entity.*;
import com.cma.cmabackend.entity.re.ReUser;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Resource
    LabEmpDao labEmpDao;

    @Resource
    UserDao userDao;

    public Map<String,Object> add(User user, int type) {
        Map<String,Object> result = new HashMap<>();
        if(userDao.findByUsername(user.getUsername())!=null){
            result.put("结果","添加失败：已经有相同用户名");
            return result;
        }
        if (type == 0) {
            LabAdmin new_u = new LabAdmin(user);
            userDao.save(new_u);
        } else if (type == 1) {
            LabEmp new_u = new LabEmp(user);
            userDao.save(new_u);
        } else if (type == 2) {
            EquipmentDept new_u = new EquipmentDept(user);
            userDao.save(new_u);
        } else if (type == 3) {
            QualityDept new_u = new QualityDept(user);
            userDao.save(new_u);
        }
        result.put("结果","添加成功");
        return result;
    }

    public List<ReUser> getAllLabEmp() {
        List<LabEmp> labEmpList = labEmpDao.findAll();
        List<ReUser> reUserList = new ArrayList<>();
        for (LabEmp i : labEmpList) {
            ReUser x = new ReUser(i.getId(), i.getType(), i.getUsername());
            reUserList.add(x);
        }
        return reUserList;
    }
}
